<template>
  <div>
    <el-card>
      <div slot="header">
        <el-row :gutter="24">
          <el-col :span="12">
            <strong>用户管理模块</strong>
          </el-col>
          <el-col :span="12">
            <el-row class="frx">
              <el-button type="primary" @click="openAdd">新增用户</el-button>
            </el-row>
          </el-col>
        </el-row>
        <el-row class="flex_item mt_5" :gutter="24">
          <el-col :span="6">
            <el-row :gutter="24" class="flex_item aic">
              <el-col :span="6">
                <span>学校</span>
              </el-col>
              <el-col :span="18">
                <el-input v-model="school"></el-input>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row :gutter="24" class="flex_item aic">
              <el-col :span="6">
                <span>用户姓名</span>
              </el-col>
              <el-col :span="18">
                <el-input v-model="username"></el-input>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="6">
            <el-row :gutter="24" class="flex_item aic">
              <el-col :span="8">
                <span>登录名</span>
              </el-col>
              <el-col :span="16">
                <el-input v-model="loginName"></el-input>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table :data="source">
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="loginName" label="登录名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="school" label="学校/单位"></el-table-column>
        <el-table-column label="地址" width="200px">
          <template slot-scope="scope">
            {{ `${scope.row.province} ${scope.row.city}` }}
          </template>
        </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">{{
            parseType(scope.row.roleId)
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-row>
              <el-button
                type="primary"
                size="mini"
                @click="openEdit(scope.row.id)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="openDelete(scope.row.id)"
                >删除</el-button
              >
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="pageSize"
        :current-page.sync="pageNum"
        :total="total"
        @current-change="loadSource"
      ></el-pagination>
    </el-card>
    <NDeleteDialog
      :visible.sync="showDelete"
      title="你确定要删除该用户？"
      :delMethods="deleteUser"
    ></NDeleteDialog>
    <AddUserDialog :visible.sync="showAdd"></AddUserDialog>
    <EditUserDialog :visible.sync="showEdit" :id="editId"></EditUserDialog>
  </div>
</template>

<script>
import NDeleteDialog from '../../../components/ndialog/delete/NDeleteDialog.vue'
import AddUserDialog from '../../../components/ndialog/add/AddUserDialog.vue'
import EditUserDialog from '../../../components/ndialog/edit/EditUserDialog.vue'
import UApi from '../../../api/UserApi.js'
export default {
  data() {
    return {
      pageSize: 15,
      pageNum: 1,
      loginName: '',
      username: '',
      school: '',
      total: 0,
      deleteId: null,
      editId: null,
      showAdd: false,
      showDelete: false,
      showEdit: false,
      source: [
        {
          id: 2,
          username: '评审专家2号',
          loginName: 'zhuanjia2',
          password: '123',
          email: 'afsdaf',
          roleId: 3,
          school: 'fdasf',
          province: '江苏',
          city: '徐州'
        },
        {
          id: 2,
          username: '评审专家2号',
          loginName: 'zhuanjia2',
          password: '123',
          email: 'afsdaf',
          roleId: 3,
          school: 'fdasf',
          province: '江苏',
          city: '徐州'
        }
      ],
      options: [
        {
          label: '管理员',
          value: 1
        },
        {
          label: '教师',
          value: 2
        },
        {
          label: '评审专家',
          value: 3
        },
        {
          label: '高校学生',
          value: 4
        },
        {
          label: '高职学生',
          value: 5
        },
        {
          label: '中小学生',
          value: 6
        },
        {
          label: '企业培训用户',
          value: 7
        }
      ]
    }
  },
  components: {
    NDeleteDialog,
    AddUserDialog,
    EditUserDialog
  },
  methods: {
    parseType(value) {
      for (let index in this.options) {
        if (this.options[index].value === value) {
          return this.options[index].label
        }
      }
    },
    loadParams() {
      return {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        loginName: this.loginName,
        username: this.username,
        school: this.school
      }
    },
    loadSource() {
      this.axios
        .get(UApi.findByParams, { params: this.loadParams() })
        .then(res => {
          if (res.data.status === 0) {
            this.total = res.data.data.total
            this.source = res.data.data.list
            // window.console.log(res.data.data);
          }
        })
    },
    openAdd() {
      this.showAdd = true
    },
    openDelete(id) {
      this.deleteId = id
      this.showDelete = true
    },
    openEdit(id) {
      this.editId = id
      this.showEdit = true
    },
    deleteUser() {
      this.axios
        .delete(UApi.deleteUser, { params: { id: this.deleteId } })
        .then(res => {
          if (res.data.status === 0) {
            this.loadSource()
          }
        })
    },
    search() {
      this.pageNum = 1
      this.loadSource()
    },
    reset() {
      this.loginName = ''
      this.school = ''
      this.username = ''
      this.loadSource()
    }
  },
  mounted() {
    this.loadSource()
  }
}
</script>

<style></style>
